package com.tubi.ArraySearch;
    //二分查找
public class ArraySecrch1 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int number = 3;
        int index =  ArraySearchIndex(arr,number); //option + 回车生成代码
        System.out.println(index);


    }

    private static int ArraySearchIndex(int[] arr, int number) {

        //定义查找范围
        int min = 0;
        int max = arr.length -1;
        //循环查找min《= max
        while (min <= max){
            //计算中间位置
            int mid = (min + max) >> 1;
            //min 指向元素 > number
                if(arr[mid]>number){
                    max = mid -1;
                }else if(arr[mid] < number){
                    //min 指向元素 < number
                        min = mid +1;
                }else {
                    //min 指向元素 = number
                        return mid;
                }
        }
        return -1;
    }
}
